package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.hutool.http.HttpUtil;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    // 添加这个方法
    private Map<String, Object> getWechatConfig() {
        if (ObjectUtils.isEmpty(appId) || ObjectUtils.isEmpty(secret)) {
            throw new BaseException("微信配置参数未正确初始化");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("appid", appId);
        params.put("secret", secret);
        return params;
    }

    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
@Value("${wechat.appId}")
    private String appId ;
    @Value("${wechat.secret}")
    private String secret;
    @Override
    public String getOpenId(String code) {
        /*封装数据类型*/
        Map<String,Object> params = new HashMap<>();
        params.put("appid", appId);
        params.put("secret", secret);
        params.put("js_code", code);
        //  发送请求,  获取结果
        String result = HttpUtil.get(REQUEST_URL, params);
        // 解析结果
        JSONObject resultObj = JSONUtil.parseObj(result);
        Integer errcode = resultObj.getInt("errcode");
        if (errcode != null ) {
            throw new BaseException( resultObj.getStr("errmsg"));
        }

        return resultObj.getStr("openid");
    }

    @Override
    public String getPhone(String phoneCode) {

        //调用微信获取token
        String token = getToken();

       Map< String, Object > params = new HashMap<>(1);
        params.put("code", phoneCode);
        String result = HttpUtil.post(PHONE_REQUEST_URL + token,JSONUtil.toJsonStr(params));
        JSONObject resultObj = JSONUtil.parseObj(result);
        if(resultObj.getInt("errcode") !=0){
            throw new BaseException(resultObj.getStr("errmsg"));
        }

        return resultObj.getJSONObject("phone_info").getStr("phoneNumber");
    }

    private String getToken() {
        Map<String, Object> params = new HashMap<>();
        params.put("appid", appId);
        params.put("secret", secret);

        //调用微信获取token
        String result = HttpUtil.get(TOKEN_URL, params);
        //解析结果
        JSONObject resultObj = JSONUtil.parseObj(result);
        if (ObjectUtil.isNotEmpty(resultObj.getInt("errcode"))) {
            //获取失败
            throw new BaseException(resultObj.getStr("errmsg"));
        }

        return resultObj.getStr("access_token");
    }


}
